<?php

//หากมีการเรียกไฟล์นี้โดยตรง
if(stristr(htmlentities($_SERVER['PHP_SELF']), "class_db.php"))
{
    Header("Location: ../index.php");
    //echo 'Access Denie';
    die();
}

//Class Name : DB
//Details : class ที่ใช้ในการเชื่อมต่อฐานข้อมูลและใช้คำสั่ง SQL ต่างๆ เช่น การ insert Update Delete ฯลฯ เป็นต้น

$db = new DB(DB_HOST, DB_NAME, DB_USERNAME, DB_PASSWORD);

class DB
{

    //ส่วนของการเชื่อมต่อ
    var $host;
    var $database;
    var $connect_db;
    var $selectdb;
    var $db;
    var $sql;
    var $table;
    var $where;

    ////////////////////// ฟังก์ชั่นต่างๆ //////////////////////
    //เชื่อมต่อดาต้าเบส
    function DB($host, $db_name, $user, $pwd)
    {
        $this->host = $host;
        $this->database = $db_name;
        $this->username = $user;
        $this->password = $pwd;
        $this->connect_db = mysql_connect($this->host, $this->username, $this->password) or $this->_error();
        $this->db = mysql_select_db($this->database, $this->connect_db) or $this->_error();
        mysql_query('SET NAMES utf8');
        //  mysql_query('SET character_set_results=utf8');
        return true;
    }

    //ปิดการเชื่อมต่อดาต้าเบส
    function closedb()
    {
        mysql_close($this->connect_db) or $this->_error();
    }

    //เพิ่มข้อมูล
    //$db->add_db("table",array("field"=>"value"));
    function add_db($table = "table", $data = "data")
    {
        $key = array_keys($data);
        $value = array_values($data);
        $sumdata = count($key);
        for($i = 0; $i < $sumdata; $i++)
        {
            if(empty($add))
            {
                $add = "(";
            }
            else
            {
                $add = $add.",";
            }
            if(empty($val))
            {
                $val = "(";
            }
            else
            {
                $val = $val.",";
            }
            $add = $add.$key[$i];
            $val = $val."'".$value[$i]."'";
        }
        $add = $add.")";
        $val = $val.")";
        $sql = "INSERT INTO ".$table." ".$add." VALUES ".$val;
        if(mysql_query($sql))
        {
            return true;
        }
        else
        {
            $this->_error();
            return false;
        }
    }

    //แก้ไขข้อมูลแบบหลายฟิลล์
    //$db->update_db("tabel",array("field"=>"value"),"where");
    function update_db($table = "table", $data = "data", $where = "where")
    {
        $key = array_keys($data);
        $value = array_values($data);
        $sumdata = count($key);
        $set = "";
        for($i = 0; $i < $sumdata; $i++)
        {
            if(!empty($set))
            {
                $set = $set.",";
            }
            $set = $set.$key[$i]."='".$value[$i]."'";
        }
        $sql = "UPDATE ".$table." SET ".$set." WHERE ".$where;
        if(mysql_query($sql))
        {
            return true;
        }
        else
        {
            $this->_error();
            return false;
        }
    }

    //แก้ไขข้อมูลแบบฟิลล์เดียว
    //$db->update("table","set","where");
    function update($table = "table", $set = "set", $where = "where")
    {
        $sql = "UPDATE ".$table." SET ".$set." WHERE ".$where;
        if(mysql_query($sql))
        {
            return true;
        }
        else
        {
            $this->_error();
            return false;
        }
    }

    //ลบข้อมูล
    //$db->del("table","where");
    function del($table = "table", $where = "where")
    {
        $sql = "DELETE FROM ".$table." WHERE ".$where;
        if(mysql_query($sql))
        {
            return true;
        }
        else
        {
            $this->_error();
            return false;
        }
    }

    //นับจำนวนแถวข้อมูล
    //$db->num_rows("table","field","where");
    function num_rows($table = "table", $field = "field", $where = "where")
    {
        if($where == "")
        {
            $where = "";
        }
        else
        {
            $where = " WHERE ".$where;
        }
        $sql = "SELECT ".$field." FROM ".$table.$where;
        if($res = mysql_query($sql))
        {
            return mysql_num_rows($res);
        }
        else
        {
            $this->_error();
            return false;
        }
    }

    //Query ข้อมูล
    //$res = $db->select_query('SELECT field FROM table WHERE where');
    function select_query($sql = "sql")
    {
        if($res = mysql_query($sql))
        {
            return $res;
        }
        else
        {
            $this->_error();
            return false;
        }
    }

    //นับจำนวนแถวข้อมูล
    //$res = $db->select_query('SELECT field FROM table WHERE where');
    //$rows = $db->rows($res);
    function rows($sql = "sql")
    {
        if($res = mysql_num_rows($sql))
        {
            return $res;
        }
        else
        {
            $this->_error();
            return false;
        }
    }

    //ดึงค่า array
    //$res = $db->select_query('SELECT field FROM table WHERE where');
    //while ($arr = $db->fetch($res)) {
    //              echo $arr['a']." - ".$arr['c']."<br>\n";
    //}
    function fetch($sql = "sql")
    {
        if($res = mysql_fetch_assoc($sql))
        {
            return $res;
        }
        else
        {
            $this->_error();
            return false;
        }
    }

    //Class Fetch Row
    function fetch_row($sql = "sql")
    {
        if($res = mysql_fetch_row($sql))
        {
            return $res;
        }
        else
        {
            $this->_error();
            return false;
        }
    }

    //แสดงข้อความผิดพลาด
    function _error()
    {
        $this->error[] = mysql_errno();
    }

    function antihack($value)
    {
        return mysql_real_escape_string($value);
    }

}

?>
